/**
 * Created by liujie on 2017/10/16.
 */
var express = require("express");
var request = require('request');

var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var util = require('../../util/commonutil');
var router = express.Router();
var auth = require('../../auth/auth');
var resultJSON = require('../common/resultJSON');
var _deviceList = [
    {
        "accountCode":"1001",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    },
    {
        "accountCode":"1002",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    },
    {
        "accountCode":"1003",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    },
    {
        "accountCode":"1004",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    },
    {
        "accountCode":"1005",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    },
    {
        "accountCode":"1006",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    },
    {
        "accountCode":"1007",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    },
    {
        "accountCode":"1008",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    },
    {
        "accountCode":"1009",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    },
    {
        "accountCode":"1010",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    },
    {
        "accountCode":"1011",
        "empName":"张三",
        "userName":"李四",
        "date":"2017-10-16"
    }
];


router.get("/",function (req, res, next) {
    res.render("dataManage/accountManage",{parentMenuList:false,sysMap:false})
});
router.post("/queryAccountList",function (req, res, next) {
    var jsonResult = {
        total:0,
        rows:[]
    };
    var sid=auth.getSessionID(req, res, next);
    var qs=req.body;
    qs.sid=sid;
    // qs["deptid"] = 1;
    request({
        url:pro.devServeraddr + "heb/sysEmp/list",
        method: "POST",
        qs:qs
    },function (error, response, body) {
        console.log("账号列表数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);

            if(result.success == true){
                console.log("rows:"+result.rows.length);
                jsonResult.total = result.total;
                jsonResult.rows = result.rows;
            }else{
                jsonResult.isSuccess=false;
                jsonResult.message="查询数据异常，请稍后重试！"
            }
        }else{
            jsonResult.isSuccess=false;
            jsonResult.message="查询数据异常，请稍后重试！"
        }
        res.json(jsonResult);
    });
   // res.json(_deviceList)
});
router.get("/addUserInfo",function (req,res,next) {
    res.render("dataManage/addUserData");
});
router.get("/viewAccountPage",function (req,res,next) {
    res.render("dataManage/dataManage")
});

router.post("/deleteAccount",function (req, res, next) {
    request({
        url:pro.devServeraddr + "heb/sysEmp/delete",
        method: "POST",
        qs: req.body
    },function (error, response, body) {
        console.log("删除账号数据：>>"+body);
        if(!error && response.statusCode == 200){
            var result = JSON.parse(body);
            if(result.success == true){
                resultJSON.isSuccess = true;
                resultJSON.data = result.data;
            }else{
                resultJSON.isSuccess=false;
                resultJSON.message="删除数据异常，请稍后重试！"
            }
        }else{
            resultJSON.isSuccess=false;
            resultJSON.message="删除数据异常，请稍后重试！"
        }
        res.json(resultJSON);
    });
});


module.exports = router;